<?php
/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2018/1/29
 * Time: 9:33
 */

namespace SDXApi\Controller;


class ActivityMamaController extends AppController
{
    public function _initialize($apptoken, $apptime) {
        parent::_initialize ( $apptoken, $apptime ); //
    }

    /**
     * 领取红包
     * @author yangzhic
     */
    public function get_bonus(){
        $phone  = I('post.phone');
        $phone = trim($phone);
        $user_id = I('post.uid',0,'int');
        $bonus_id = I('post.bonus_id',0,'int');

        if(empty($phone)){
            returnApiError('请输入手机号');
        }

        if(strlen($phone)!=11){
            returnApiError('手机号长度不正确');
        }

        if(empty($user_id)){
            returnApiError('参数错误');
        }

        if(empty($bonus_id)){
            returnApiError('参数错误不能为空');
        }

        $res = $this->set_user_phone($phone,$user_id);
        if(!$res){
            returnApiError('验证联系手机号错误，请重试');
        }

        $res = $this->send_bonus($user_id,$bonus_id);

        if($res['error']==1){
            returnApiError($res['msg']);
        }else{
            returnApiSuccess($res);
        }
    }

    private function set_user_phone($phone,$user_id){
        $map = array('user_id'=>$user_id);
        $users = M('users');
        $user_info = $users->where($map)->find();

        $flag = true;
        if(!empty($user_info)){
            if(empty($user_info['mobile_phone'])){
                $res = $users->where($map)->setField('mobile_phone',$phone);
                if($res===false){
                    $flag = false;
                }
            }
        }

        return $flag;
    }

    /**
     * 发放红包
     * @author yangzhic
     * @param $user_id
     * @param $bonus_id
     * @return array
     */
    public function send_bonus($user_id,$bonus_id){
        $res = array('error'=>0,'msg'=>'');

        if(empty($user_id) || empty($bonus_id)){
            $res['error'] = 1;
            $res['msg'] = "参数错误";
        }else{
            $res = $this->check_get_bonus($user_id,$bonus_id);
            if($res['error']!=1){
                $res = D('Userbonus')->auto_get_bonus($bonus_id,$user_id);
            }
        }

        return $res;
    }

    /**
     * 检查是否已领取
     * @author yangzhic
     * @param $user_id
     * @param $bonus_id
     * @return array
     */
    private function check_get_bonus($user_id,$bonus_id){
        $res = array('error'=>0,'msg'=>'');

        $activity_config = C('activity_mama_net');

        $bonus_list = $activity_config['bonus_id'];

        if(!in_array($bonus_id,$bonus_list)){
            $res['error'] = 1;
            $res['msg'] = "没有此红包";
        }else{
            $map = array();
            $map['user_id'] = $user_id;
            $map['bonus_type_id'] = array('in',$bonus_list);

            $count = M("user_bonus")->where($map)->count('bonus_id');

            if($count>0){
                $res['error'] = 1;
                $res['msg'] = "已领取过红包";
            }
        }

        return $res;
    }

    /**
     * 提交收货人信息
     * @author yangzhic
     */
    public function add_receipt_info(){
        $user_id = I('post.uid',0,'int');
        $name = I('post.name');
        $phone = I('post.phone');
        $address = I('post.address');
        $email = I('post.email');

        if(empty($name)){
            returnApiError('请填写姓名！');
        }

        if(empty($address)){
            returnApiError('请填写收货地址！');
        }

        $res = $this->check_user($user_id);
        if($res['error']==1){
            returnApiError($res['msg']);
        }

        $res = $this->check_phone($phone);
        if($res['error']==1){
            returnApiError($res['msg']);
        }

        $res = $this->check_email($email);
        if($res['error']==1){
            returnApiError($res['msg']);
        }

        $data = array();
        $data['user_id'] = $user_id;
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['address'] = $address;
        $data['email'] = $email;
        $data['add_time'] = gmtime();

        $res = M('ActivityMamanet')->add($data);

        if($res){
            returnApiSuccess('','信息提交成功');
        }else{
            returnApiError('信息提交失败，请重试!');
        }

    }

    /**
     * 检查手机号
     * @author yangzhic
     * @param $phone
     * @return array
     */
    private function check_phone($phone){
        $res = array('error'=>0,'msg'=>'');

        if(empty($phone)){
            $res['error'] = 1;
            $res['msg'] = "请填写联系手机号";
        }elseif(strlen(trim($phone))!==11){
            $res['error'] = 1;
            $res['msg'] = "联系手机号长度不正确";
        }else{
            $user = D('ActivityMamanet')->getDataByPhone($phone);
            if(!empty($user)){
                $res['error'] = 1;
                $res['msg'] = "当前手机号已经提交过信息，不可重复提交!";
            }
        }
        return $res;
    }

    /**
     * 检查用户
     * @author yangzhic
     * @param $user_id
     * @return array
     */
    private function check_user($user_id){
        $res = array('error'=>0,'msg'=>'');

        if(empty($user_id)){
            $res['error'] = 1;
            $res['msg'] = "user_id不能为空";
        }else{
            $user = D('ActivityMamanet')->getDataByUserId($user_id);
            if(!empty($user)){
                $res['error'] = 1;
                $res['msg'] = "当前用户已经提交过信息，不可重复提交!";
            }
        }
        return $res;
    }

    /**
     * 检查邮箱
     * @author yangzhic
     * @param $email
     * @return array
     */
    private function check_email($email){
        $res = array('error'=>0,'msg'=>'');

        if(!empty($email)){
            if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
                $res['error'] = 1;
                $res['msg'] = "无效的 email 格式！";
            }else{
                $user = D('ActivityMamanet')->getDataByEmail($email);
                if(!empty($user)){
                    $res['error'] = 1;
                    $res['msg'] = "当前邮箱已经提交过信息，不可重复提交!";
                }
            }
        }

        return $res;
    }
}